#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Clone this Directory                                               #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 03/29/2011                                             #
# Last Modification: 03/29/2011                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 83
#
# MANUAL: This script will create a copy of the current image directory on the same level, and copy the current database file 2dx_image.cfg into that new cloned directory. 
#
# MANUAL: The new image directory's database will be populated with a different filename and image number, as each imagenumber can only appear once. The parameter dialogue below allows you to specify the new image number. The new image name will be generated automatically.
#
# DISPLAY: new_imagenumber
# DISPLAY: doswap
# DISPLAY: comment
# DISPLAY: imagename
# DISPLAY: nonmaskimagename
# DISPLAY: imagenumber
# DISPLAY: crop
# DISPLAY: comment
# DISPLAY: imagesidelength
# DISPLAY: lattice
# DISPLAY: secondlattice
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set comment = ""
set imagenumber = ""
set imagename = ""
set nonmaskimagename = ""
set crop = ""
set comment = ""
set imagesidelength = ""
set lattice = ""
set secondlattice = ""
set new_imagenumber = ""
set doswap = ""
#
#$end_vars
#
echo bin_2dx = ${bin_2dx}
echo proc_2dx = ${proc_2dx}
#
echo "<<@progress: 1>>"
#
set scriptname = 2dx_clone
\rm -f LOGS/${scriptname}.results
#
set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#
source ${proc_2dx}/2dx_makedirs
#
############################################################################# 
############################################################################# 
#
set old_imagedirectory = `basename $PWD`
#
set tlen = `echo ${old_imagedirectory} | wc -c`
set tlen2 = `echo ${tlen} | awk '{ s = abs ( $1 - 2 ) } END { print s }'`
set tlen3 = `echo ${tlen} | awk '{ s = abs ( $1 - 3 ) } END { print s }'`
set tlen4 = `echo ${tlen} | awk '{ s = abs ( $1 - 4 ) } END { print s }'`
set tend = `echo ${old_imagedirectory} | cut -c ${tlen3}-${tlen2}`
if ( ${tend} == "_c" ) then
  set old_imagedirectory = `echo ${old_imagedirectory} | cut -c 1-${tlen4}`
endif
#
set clone_number = "1"
set new_imagedirectory = ${old_imagedirectory}_c${clone_number}
while ( -d ../${new_imagedirectory} )
  set clone_number = `echo ${clone_number} | awk '{ s = $1 + 1 } END { print s }'`
  set new_imagedirectory = ${old_imagedirectory}_c${clone_number}
end
#
set new_imagename = ${imagename}_c${clone_number}
set new_nonmaskimagename = ${nonmaskimagename}_c${clone_number}
#
echo ":: "
${proc_2dx}/linblock "Preparing to clone into new directory ${new_imagedirectory}"
echo ":: "
#
set err_val = 0
#
if ( ${new_imagenumber} == "PutUniqueNumberHere" ) then
  ${proc_2dx}/linblock "ERROR: You didn't specify a new unique image number."
  set err_val = 1
endif
#
if ( ${new_imagedirectory} == ${old_imagedirectory} ) then
  ${proc_2dx}/linblock "ERROR: Directory ${new_imagedirectory} already exists."
  set err_val = 1
endif
#
if ( ${new_imagename} == ${imagename} ) then
  ${proc_2dx}/linblock "ERROR: Old and New imagename ${imagename} cannot be the same."
  set err_val = 1
endif
#
if ( ${new_imagenumber} == ${imagenumber} ) then
  ${proc_2dx}/linblock "ERROR: Old and New imagenumber ${imagenumber} cannot be the same."
  set err_val = 1
endif
#
cd ..
if ( -d ${new_imagedirectory} ) then
  ${proc_2dx}/linblock "ERROR: Directory ${new_imagedirectory} already exists"
  set err_val = 1
endif
#
if ( ${err_val} == 1 ) then
  echo ":: "
  ${proc_2dx}/protest "ABORTING..."
endif
#
if ( ${doswap} == "0" ) then
  set new_lattice = `echo ${secondlattice} | sed 's/, /,/g'`
  set new_secondlattice = `echo ${lattice} | sed 's/, /,/g'`
else
  set new_lattice = `echo ${lattice} | sed 's/, /,/g'`
  set new_secondlattice = `echo ${secondlattice} | sed 's/, /,/g'`
endif
#
\mkdir ${new_imagedirectory}
cd ${new_imagedirectory}
source ${proc_2dx}/2dx_makedirs
cp ../${old_imagedirectory}/2dx_image.cfg .
#
cat << eot > sed.tmp
s/set imagename = "${imagename}"/set imagename = "${new_imagename}"/
s/set nonmaskimagename = "${nonmaskimagename}"/set nonmaskimagename = "${new_nonmaskimagename}"/
s/set imagenumber = "${imagenumber}"/set imagenumber = "${new_imagenumber}"/
s/set lattice = "${lattice}"/set lattice = "${new_lattice}"/
s/set secondlattice = "${secondlattice}"/set secondlattice = "${new_secondlattice}"/
eot
cat 2dx_image.cfg | sed -f sed.tmp > tmp.cfg
mv -f tmp.cfg 2dx_image.cfg
\rm sed.tmp
#
${proc_2dx}/linblock "Cloned database 2dx_image.cfg created and adapted."
#
if ( -e  ../${old_imagedirectory}/${nonmaskimagename}.tif ) then
  \cp -f ../${old_imagedirectory}/${nonmaskimagename}.tif ${new_nonmaskimagename}.tif
endif
if ( -e  ../${old_imagedirectory}/${imagename}.tif ) then
  \cp -f ../${old_imagedirectory}/${imagename}.tif ${new_imagename}.tif
endif
if ( -e  ../${old_imagedirectory}/${nonmaskimagename}_raw.mrc ) then
  \cp -f ../${old_imagedirectory}/${nonmaskimagename}_raw.mrc ${new_nonmaskimagename}_raw.mrc
endif
if ( -e  ../${old_imagedirectory}/${imagename}.mrc ) then
  \cp -f ../${old_imagedirectory}/${imagename}.mrc ${new_imagename}.mrc
endif
#
${proc_2dx}/linblock "Links to old image files in cloned directory created."
#
if ( ${doswap} == "0" ) then
  if ( -e  ../${old_imagedirectory}/${nonmaskimagename}.spt ) then
    cp -f ../${old_imagedirectory}/${nonmaskimagename}.spt ${new_imagename}.spt
  endif
  ${proc_2dx}/linblock "Old spotlist copied to new spotlist file"
endif
#
cd ../${old_imagedirectory}
#
############################################################################# 
############################################################################# 
#
#
echo "<<@progress: 100>>"
#
############################################################################# 
echo ":: "
${proc_2dx}/linblock "Cloning finished correctly."
echo ":: "
echo "::     Now go to 2dx_merge, refresh the display list, "
echo "::     and open the cloned image from 2dx_merge"
echo ":: "
#############################################################################
